var coordDisplayMode = "user";
var updateTime = "11/28/11 2:29pm"
var gpsRegex = /(-?\d+\.?\d*),\s?(-?\d+\.?\d*)/g
var locationData = [
	{
		a: "34.1309129301284",
		b: "-117.0077",
		name: "Hunting Grounds",
		count:16,
	},
	{
		a: "39.4523780281434",
		b: "-80.95745",
		name: "Q*Bert",
		count:1,
	},
	{
		a: "42.3859304155381",
		b: "-77.0125",
		name:"Watkins Glen",
		count:3,
	},
	{
		a:"34.10367532072881",
		b:"-118.27385",
		name:"Los Angeles",
		count:3,
	},
	{
		a: "47.4566158496044",
		b: "-122.1463",
		name: "Renton 7",
		count:1,
	},
	{
		a: "30.100194505207465",
		b: "-95.65355",
		name: "Dragonfly",
		count:2,
	},
	{
		a: "41.1205993492012",
		b: "-73.4895",
		name: "Waveny Park",
		count:2,
	},
	{
		a: "30.0650631939436",
		b: "-94.07745",
		name: "Beaumont",
		count:1,
	},
	{
		a: "43.7160983115838",
		b: "-79.3457",
		name: "Toronto",
		count:1,
	},
	{
		a: "40.4932754174841",
		b: "-78.5764",
		name: "Gallitzin",
		count:1,
	},
	{
		a: "40.4940358951315",
		b: "-78.5735",
		name: "Gallitzin 2",
		count:2,
	},
	{
		a: "36.9365620875085",
		b: "-121.8602",
		name: "Watsonville",
		count:1,
	},
	{
		a: "43.9810565643104",
		b: "-90.4954",
		name: "Tomah",
		count:1,
	},
	{
		a: "42.0643281786285",
		b: "-93.89615",
		name: "Boone",
		count:1,
	},
	{
		a: "37.9698860002426",
		b: "-75.43125",
		name: "Horntown",
		count:1,
	},
	{
		a: "40.4963933210651",
		b: "-78.60025",
		name: "Loretto",
		count:3,
	},
	{
		a: "37.6691543419343",
		b: "-121.8795",
		name: "Pleasanton",
		count:1,
	},
	{
		a: "32.4464047562883",
		b: "-89.07365",
		name: "Decatur",
		count:1,
	},
	{
		a: "29.8292059277038",
		b: "-95.29665",
		name: "Houston",
		count:2,
	},
	{
		a: "30.2276352128209",
		b: "-89.09255",
		name: "Cat Island",
		count:1,
	},
	{
		a: "41.7093689452417",
		b: "-88.30915",
		name: "Oswego",
		count:2,
	},
	{
		a: "28.4175690826868",
		b: "-81.57985",
		name: "Orlando",
		count:1,
	},
	{
		a: "39.1742360830135",
		b: "-85.583",
		name: "Westport",
		count:1,
	},
	{
		a: "47.6132482390582",
		b: "-122.0345",
		name: "Sammamish",
		count:1,
	},
	{
		a: "37.7662142538153",
		b: "-122.28785",
		name: "Alameda",
		count:7,
	},
	{
		a: "47.068844961545",
		b: "-82.0628",
		name: "Winnie Lake",
		count:1,
	},
	{
		a: "38.8796554308895",
		b: "-77.0392",
		name: "Washington DC 2",
		count:1,
	},
	{
		a: "37.8493297755403",
		b: "-121.93115",
		name: "Danville",
		count:5,
	},
	{
		a: "26.5437359039522",
		b: "-81.952",
		name: "Cape Coral",
		count:1,
	},
	{
		a: "46.9408456144348",
		b: "-94.89465",
		name: "Nevis",
		count:1,
	},
	{
		a: "39.1805151526012",
		b: "-85.56755",
		name: "Westport 2",
		count:1,
	},
	{
		a: "40.7635363050189",
		b: "-73.98775",
		name: "New York",
		count:1,
	},
	{
		a: "30.2885305877486",
		b: "-95.5722",
		name: "Montgomery",
		count:1,
	},
	{
		a: "39.9581387013903",
		b: "-75.17185",
		name: "Philadelphia",
		count:1,
	},
	{
		a: "38.6072955443128",
		b: "-122.8644",
		name: "Healdsburg",
		count:2,
	},
	{
		a: "39.9616645732609",
		b: "-75.60155",
		name: "West Chester 3",
		count:1,
	},
	{
		a: "39.9572955311813",
		b: "-75.6108",
		name: "West Chester",
		count:1,
	},
	{
		a: "42.8682283110735",
		b: "-112.43855",
		name:"Pocatello",
		count:17,
	},
	{
		a: "25.9165400774372",
		b: "-109.168",
		name:"Ahome",
		count:4,
	},
	{
		a: "34.1043377374752",
		b: "-117.75125",
		name:"Pomona",
		count:1,
	},
	{
		a:"30.35051002528852",
		b:"-97.4311",
		name:"Elgin",
		count:2,
	},
	{
		a:"45.83094801419032",
		b:"-122.9653",
		name:"Scappoose",
		count:3,
	},
	{
		a:"30.3668184258974",
		b:"-97.94745",
		name:"Austin",
		count:1,
	},
	{
		a:"39.478471900083",
		b:"-76.79365",
		name:"Reisterstown",
		count:1,
	},
	{
		a:"47.4768963777336",
		b:"-121.9938",
		name:"Issaquah",
		count:2,
	},
	{
		a:"30.687446564693264",
		b:"-88.23855625",
		name:"Mobile",
		count:1,
	},
	{
		a:"30.671945969907565",
		b:"-88.22855",
		name:"Mobile 2",
		count:1,
	},
	{
		a: "47.45627777450739",
		b: "-122.15115",
		name: "Renton 5",
		count:1,
	},
	{
		a:"33.98311208048642",
		b:"-118.4717",
		name:"Venice",
		count:2,
	},
	{
		a:"37.12873086793958",
		b:"-80.42325625",
		name:"Christiansburg",
		count:1,
	},
	{
		a:"30.4537706145504",
		b:"-86.58365625",
		name:"Shalimar",
		count:1,
	},
	{
		a:"35.90199653996088",
		b:"-86.35265000000001",
		name:"Murfreesboro",
		count:4,
	},
	{
		a:"24.52667993938716",
		b:"-81.97150625",
		name:"Key West",
		count:1,
	},
	{
		a:"26.45139674502199",
		b:"-77.18810625",
		name:"The Bahamas",
		count:1,
	},
	{
		a:"42.3724322022329",
		b:"-87.91165625",
		name:"Gurnee",
		count:1,
	},
	{
		a:"41.183491987850566",
		b:"-96.20975625",
		name:"Omaha",
		count:1,
	},
	{
		a:"31.091124031944467",
		b:"-92.91575625",
		name:"Pitkin",
		count:1,
	},
	{
		a:"38.887615338985405",
		b:"-77.04340625",
		name:"Washington DC",
		count:1,
	},
	{
		a:"41.123029950594415",
		b:"-73.48470625",
		name:"Waveny Park 2",
		count:2,
	},
	{
		a:"41.268481085416624",
		b:"-96.14575625",
		name:"Omaha",
		count:1,
	},
	{
		a:"33.811812587873774",
		b:"-117.91890625",
		name:"Anaheim",
		count:1,
	},
	{
		a:"42.359354440411735",
		b:"-71.06310625",
		name:"Boston",
		count:1,
	},
	{
		a:"39.99762",
		b:"-76.1245",
		name:"Paradise",
		count:1,
	},
	{
		a:"47.45765006185351",
		b:"-122.15575625",
		name:"Renton 2",
		count:1,
	},
	{
		a:"42.0811982600297",
		b:"-70.81320625",
		name:"Pembroke",
		count:1,
	},
	{
		a:"30.413936304785565",
		b:"-97.60505625",
		name:"Pflugerville",
		count:1,
	},
	{
		a:"47.48250594195336",
		b:"-122.21495",
		name:"Renton",
		count:2,
	},
	{
		a:"47.45893470261904",
		b:"-122.15140625",
		name:"Renton 3",
		count:1,
	},
	{
		a:"35.13988901866067",
		b:"-79.01055625",
		name:"Fort Bragg",
		count:1,
	},
	{
		a:"33.83638279727036",
		b:"-118.2902",
		name:"Torrance",
		count:2,
	},
	{
		a:"35.961287861703255",
		b:"-83.91975625",
		name:"Knoxville",
		count:1,
	},
	{
		a:"40.048777133548825",
		b:"-75.19070625",
		name:"Philadelphia",
		count:1,
	},
	{
		a:"39.9706517",
		b:"-74.6837062",
		name:"Pemberton",
		count:1,
	},
	{
		a:"43.71908160662557",
		b:"-79.34175625",
		name:"Toronto 2",
		count:1,
	},
	{
		a:"37.7682896",
		b:"-122.2551562",
		name:"Alameda",
		count:1,
	},
	{
		a:"44.990745550972086",
		b:"-93.26705625",
		name:"Minneapolis",
		count:1,
	},
	{
		a:"47.4610306280645",
		b:"-122.15115625",
		name:"Renton 4",
		count:1,
	},
	{
		a:"39.2188950586841",
		b:"-76.64910625",
		name:"Linthicum Heights",
		count:1,
	},
	{
		a:"40.74120893912119",
		b:"-73.59370625",
		name:"Westbury",
		count:1,
	},
	{
		a:"38.960978538125694",
		b:"-90.92385625",
		name:"Moscow Mills",
		count:1,
	},
	{
		a:"37.80725065634853",
		b:"-122.25730625",
		name:"Oakland",
		count:1,
	},
	{
		a:"47.4580557",
		b:"-122.1498562",
		name:"Renton 6",
		count:1,
	},
	{
		a:"34.07164471821251",
		b:"-117.73000625",
		name:"Pomona 2",
		count:1,
	},
	{
		a:"40.74689118486634",
		b:"-89.64255625",
		name:"Peoria",
		count:1,
	},
	{
		a:"44.0905658",
		b:"-69.0730562",
		name:"Owls Head",
		count:1,
	},
	{
		a:"30.6755783306396",
		b:"-87.96955625",
		name:"Spanish Fort",
		count:1,
	},
	{
		a:"39.9584653061614",
		b:"-75.60595625",
		name:"West Chester 2",
		count:3,
	},
	{
		a:"33.80873831452295",
		b:"-117.53495625",
		name:"Corona",
		count:1,
	},
	{
		a:"47.4535930954799",
		b:"-122.15165625",
		name:"Renton",
		count:1,
	},
	{
		a:"43.72045479122108",
		b:"-79.34630625",
		name:"Toronto",
		count:1,
	},
	{
		a:"44.9875629681856",
		b:"-93.26240625",
		name:"Minneapolis",
		count:1,
	},
	{
		a:"37.2664552107468",
		b:"-76.75340625",
		name:"Williamsburg",
		count:1,
		classname:"new",
	},
	{
		a:"47.6160321046006",
		b:"-122.31565625",
		name:"Seattle",
		count:4,
		classname:"new",
	},
];

locationData.sort(function(a,b){return a.b - b.b;});


function geoLink(lat, lng, name){
	var geo = $('<a class="locationLink">'+name+'</a>').click({"lat": lat, "lng":lng}, function(a) {$(this).css("color", "purple"), goToLatLng(a.data.lat, a.data.lng);});
	return geo;
}

function checkTravelForGPS(a){
	var gpsMatch = gpsRegex.exec($("#travelTextValue").val());
	a.keyCode == 13 ? gpsMatch ? $("#travelTextValue").val("").removeClass("locationCurrentText") && goToLatLng(gpsMatch[1], gpsMatch[2]) : travel(a) : travel(a);
}

function popupCurrentLocation(){
	var locationDisplayText;
	if (coordDisplayMode == "dev")
		locationDisplayText = "{\na:\""+gCurrentClientPos.y+"\",\nb:\""+gCurrentClientPos.x+"\",\nname:\""+gCurrentCity.name+"\",\ncount:1,\nclassname:\"new\",\n},";
	else
		locationDisplayText = "loc:"+gCurrentClientPos.y+","+gCurrentClientPos.x
	showPopupAlert("Current Location", locationDisplayText, null, null, null);
}

function loadListFromJSON(){
	$.getJSON("http://www.jsommers.com/cornucopias2011.json?jsoncallback=?",
		function(data) {
			alert("Cornucopia data loaded");
			$.each(data.locationData, function(i,l){
				var number = parseInt(i)+1;
				var locationName = l.name;
				if(!locationName){locationName = "Location "+number;}
				var link = geoLink(l.a, l.b, locationName);
				l.classname && link.css("color", "green");
				link.appendTo(corn);
				if(locationData.length != number){corn.append(", ");}
			});
		});
}

function rebuildAlpha(){
	$("#cornucopias").remove();
	locationData.sort(function(a,b){return a.name.localeCompare(b.name);});
	loadListFromLocal();
}

function loadListFromLocal(){
	var cornCount = 0;
	var corn = $('<div id="cornucopias"><b>Cornucopias</b> (updated '+updateTime+') <a href="javascript:rebuildAlpha()">Alphabetical Sort</a><br></div>');
	for(i in locationData){
		var number = parseInt(i)+1;
		var locationName = locationData[i].name;
		if(!locationName){locationName = "Location "+number;}
		(locationData[i].count > 1) && (locationName += " ("+locationData[i].count+")");
		var link = geoLink(locationData[i].a, locationData[i].b, locationName/*+" ("+locationData[i].count+")"*/);
		locationData[i].classname && link.css("color", "green");
		link.appendTo(corn);
		cornCount = cornCount + locationData[i].count;
		if(locationData.length != number){corn.append(", ");}
	}
	corn.children("b").append(' - '+cornCount+' total');
	$("#footer").prepend(corn);
}

//loadListFromJSON();
loadListFromLocal();

$("#recenter").click(popupCurrentLocation);

//intercept travel text to go jump to GPS
//$("#travelTextValue").removeAttr("onkeydown");
//$("#travelTextValue").keydown(function(event){checkTravelForGPS(event);});